package com.wee.aircoach_user;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class AccountBindActivity extends Activity implements View.OnClickListener {
  /*  private TextView btnGetCode;
    private TextView bindPhone;

    private EditText editPhone, editVerifyCode, editPwd;

    private View layoutUnbind, layoutBinded;

    private String code;

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            int time = msg.what - 1;
            if (time == 0) {
                btnGetCode.setText("获取验证码");
            } else {
                // int m = time/60;
                // int s = time%60;
                // btnSend.setText(String.valueOf(m+"分"+s + "s后重新发送"));
                btnGetCode.setText(String.valueOf("验证码已发送 " + time));
            }
        };
    };*/

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_bind);
        initView();
     /*   user = UserSingle.getInstance().getUser(this);
        if(user!=null){
            if(user.getPhoneNum()!=null && user.getPhoneNum().length()!=0){
                layoutBinded.setVisibility(View.VISIBLE);
                layoutUnbind.setVisibility(View.GONE);
                findViewById(R.id.btn_right).setVisibility(View.INVISIBLE);
                bindPhone.setText(""+user.getPhoneNum());
            }else{
                layoutBinded.setVisibility(View.GONE);
                layoutUnbind.setVisibility(View.VISIBLE);
            }
        }*/
    }

    private void initView() {
      /*  layoutUnbind = findViewById(R.id.layout_unbind);
        layoutBinded = findViewById(R.id.layout_binded);
        findViewById(R.id.back).setOnClickListener(this);
        findViewById(R.id.btn_right).setOnClickListener(this);
        bindPhone = (TextView)findViewById(R.id.bind_phone);
        editPhone = (EditText)findViewById(R.id.phone);
        editVerifyCode = (EditText)findViewById(R.id.verify_code);
        editPwd = (EditText)findViewById(R.id.pwd);
        btnGetCode = (TextView)findViewById(R.id.get_code);
        btnGetCode.setOnClickListener(this);*/
    }

    @Override
    public void onClick(View v) {
       /* if (v.getId() == R.id.back) {
            finish();
        }else if(v.getId() == R.id.btn_right){
            done();
        }else if(v.getId() == R.id.get_code){
            getCode();
        }*/
    }

    private void getCode(){
        /*String phone  = editPhone.getText().toString().trim();
        if(phone.length() == 0){
            Toast.makeText(AccountBindActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        RequestParams params = new RequestParams();
        params.put("_type","json");
        params.put("phoneNum",""+phone);

        AppProgressBar.checkAndCreateProgressBar(this);
        MyApplication.getInstance().getHttpClient().get(Constant.API_GET_CODE, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    JSONObject json = new JSONObject(new String(bytes));
                    if(json.get("status").equals("ok")){
                        code = json.getString("data");
                        btnGetCode.setClickable(false);
                        new Thread() {
                            public void run() {
                                int temp = 60;
                                while (true) {
                                    SystemClock.sleep(1000);
                                    Message msg = Message.obtain();
                                    msg.what = temp;
                                    temp--;
                                    handler.sendMessage(msg);
                                    if (temp == 0) {
                                        btnGetCode.setClickable(true);
                                        return;
                                    }
                                }
                            };
                        }.start();
                    }else{

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                AppProgressBar.closeProgressBar();
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                AppProgressBar.closeProgressBar();
            }
        });*/
    }

    private void done() {
    /*    final String phone  = editPhone.getText().toString().trim();
        String verifyCode  = editVerifyCode.getText().toString().trim();
        String pwd  = editPwd.getText().toString().trim();
        
        
        if(phone.length() == 0){
            Toast.makeText(AccountBindActivity.this, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if(code ==null ||code.length() ==0){
            Toast.makeText(AccountBindActivity.this, "未获取验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        if(verifyCode.length() == 0){
            Toast.makeText(AccountBindActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(!code.equals(verifyCode)){
            Toast.makeText(AccountBindActivity.this, "验证码不匹配", Toast.LENGTH_SHORT).show();
            return;
        }

//        if(pwd.length() == 0){
//            Toast.makeText(AccountBindActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }

        int userId = SharedPreferencesUtil.getInt(this, Constant.USERID);
        RequestParams params = new RequestParams();
        params.put("_type","json");
        params.put("id",""+userId);
        params.put("cellPhone",phone);
        params.put("role","user");

        AppProgressBar.checkAndCreateProgressBar(this);
        MyApplication.getInstance().getHttpClient().get(Constant.API_BIND_PHONE, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                try {
                    JSONObject json = new JSONObject(new String(bytes));
                    if(json.get("status").equals("ok")){
                        Toast.makeText(AccountBindActivity.this, "绑定成功", Toast.LENGTH_SHORT).show();
                        layoutBinded.setVisibility(View.VISIBLE);
                        layoutUnbind.setVisibility(View.GONE);
                        findViewById(R.id.btn_right).setVisibility(View.INVISIBLE);
                        bindPhone.setText(""+phone);

                        // update local user profile.
                        if(user!=null){
                            user.setPhoneNum(phone+"");
                            UserSingle.getInstance().setUser(AccountBindActivity.this, user);
                        }
                    }else{
                        Toast.makeText(AccountBindActivity.this, "绑定失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                AppProgressBar.closeProgressBar();
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                throwable.printStackTrace();
                AppProgressBar.closeProgressBar();
                Toast.makeText(AccountBindActivity.this, "绑定失败", Toast.LENGTH_SHORT).show();
            }
        });*/
    }
}
